package main

import "sort"

func specialArray(nums []int) int {
	sort.Slice(nums, func(i, j int) bool {
		return nums[i] < nums[j]
	})
	for x := 0; x <= len(nums); x++ {
		pivot := sort.Search(len(nums), func(i int) bool {
			return nums[i] >= x
		})
		var count int
		if pivot == -1 {
			count = 0
		} else {
			count = len(nums) - pivot
		}
		if count == x {
			return x
		}
	}
	return -1
}

func main() {
	println(specialArray([]int{2, 5}))
	println(specialArray([]int{0, 0}))
	println(specialArray([]int{0, 4, 3, 0, 4}))
	println(specialArray([]int{3, 6, 7, 7, 0}))
}
